﻿<?php
error_reporting(E_ERROR|E_PARSE);
session_start();
if($_SESSION["islogin"]!=='igiveyouthepower'){
echo "<script language=JavaScript>\r\n";
echo "alert('请先登录!');\r\n";
echo "location.href='../index.php'\r\n";
echo "</script>";
}else{
function replace_specialChar($strParam){
    $regex = str_replace("'","''",$strParam);
    return $regex;
}
$bname=replace_specialChar($_GET["bname"]);
$lang=replace_specialChar($_REQUEST["lang"]);
$platform=replace_specialChar($_GET["platform"]);
function input_csv($handle){
$out=array();
$n=0;
while($data=fgetcsv($handle,10000)){
$num=count($data);         
for($i=0;$i<$num;$i++){
$out[$n][$i] = $data[$i];
}
$n++;     
}
return $out;} 
require_once('../config.php');
$conn = mysqli_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname);
$tmp_name = $_FILES["file1"]["tmp_name"];
$name  = $_FILES["file1"]["name"];
$nname=explode('.',$name);
if(end($nname)!=="csv"){
echo "<script language=JavaScript>\r\n";
echo "alert('导入文件格式不正确!');\r\n";
echo "location.href='". $_SERVER["HTTP_REFERER"]."'\r\n";
echo "</script>";
}else{
if(empty($tmp_name)){echo '请选择要导入的CSV文件!';
exit;}     
$handle = fopen($tmp_name, 'r');     
$result = input_csv($handle); //解析csv     
$len_result = count($result);     
if($len_result==0){echo '没有任何数据!';         
exit;     
}    
$proid="";
for ($i = 1; $i < $len_result; $i++) { //循环获取各字段值         
$proname = ucwords(iconv('gb2312', 'utf-8', $result[$i][0])); //中文转码         
$price = $result[$i][1];         
$coin = $result[$i][2];
$content = iconv('gb2312', 'utf-8', $result[$i][3]); 
$skucode = $result[$i][4]; 
$bname = $result[$i][5];
$keywords = $result[$i][6]; 
$des = $result[$i][7]; 
$weight = $result[$i][8]; 
$kucun = $result[$i][9]; 
$logo1 = $result[$i][10];
$logo2 = $result[$i][11];
$logo3 = $result[$i][12];
$logo4 = $result[$i][13];
$logo5 = $result[$i][14];
$logo6 = $result[$i][15];
$logo7 = $result[$i][16];
$logo8 = $result[$i][17];
$logo9 = $result[$i][18];
$logo10 = $result[$i][19];
$sketch = $result[$i][20];
$bt = $result[$i][21];
$fromurl = $result[$i][22];
$relation = $result[$i][23];
if(empty($code)){$code=date("YmdHis").rand(100,999);}
$sql="select * from ph_".$lang." where skucode='".$skucode."'";
$resulta=mysqli_query($conn,$sql);
if(mysqli_num_rows($resulta)<1 and $relation=="parent"){

$bt = $result[$i][21];
mysqli_query($conn,"insert into ph_".$lang."(proname,price,saleprice,coin,code,content,skucode,bname,user_name,ndate,keywords,des,weight,kucun,platform,sketch,btname,fromurl)values('".$proname."','".$price."','".$price."','".$coin."','".$code."','".$content."','".$skucode."','".$bname."','".$_SESSION["managername"]."','".date("Y-m-d",time())."','".$keywords."','".$des."','".$weight."','".$kucun."','".$platform."','".$sketch."','".$bt."','".$fromurl."')");
$proid=mysqli_insert_id($conn);
if(!empty($logo1)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo1."')");
    $imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
if(!empty($logo2)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo2."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
if(!empty($logo3)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo3."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
if(!empty($logo4)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo4."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
if(!empty($logo5)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo5."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
if(!empty($logo6)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo6."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
if(!empty($logo7)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo7."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
if(!empty($logo8)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo8."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
if(!empty($logo9)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo9."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
if(!empty($logo10)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo10."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid)values('".$imgid."','".$proid."')");
}
}else{
mysqli_query($conn,"insert into ph_shuxing(cname,proid,price,saleprice,kucun,skucode)values('".$bt."','".$proid."','".$price."','".$price."','".$kucun."','".$skucode."')");
$btid=mysqli_insert_id($conn);
if(!empty($logo1)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo1."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
if(!empty($logo2)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo2."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
if(!empty($logo3)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo3."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
if(!empty($logo4)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo4."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
if(!empty($logo5)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo5."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
if(!empty($logo6)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo6."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
if(!empty($logo7)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo7."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
if(!empty($logo8)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo8."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
if(!empty($logo9)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo9."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
if(!empty($logo10)){
	mysqli_query($conn,"insert into ph_img(imgpath)values('".$logo10."')");
	$imgid=mysqli_insert_id($conn);
    mysqli_query($conn,"insert into ph_proimg(imgid,proid,btid)values('".$imgid."','".$proid."','".$btid."')");
}
}}
fclose($handle); //关闭指针       
echo "<script language=JavaScript>\r\n";
echo "alert('全部导入成功!');\r\n";
echo "location.href='". $_SERVER["HTTP_REFERER"]."'\r\n";
echo "</script>";
}}?>